<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收据单号" prop="receipt">
        <el-input
          v-model="queryParams.receipt"
          placeholder="请输入收据单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编号" prop="cusId">
        <el-input
          v-model="queryParams.cusId"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单日期" prop="date">
        <el-date-picker clearable
          v-model="queryParams.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择订单日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="付款方式" prop="payment">
        <el-input
          v-model="queryParams.payment"
          placeholder="请输入付款方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入付款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售员编号" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入销售员编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8"><el-col :span="1.5">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
        v-hasPermi="['system:orderform:add']"
      >新增</el-button>
    </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cashierList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="收据单号" align="center" prop="receipt" />
      <el-table-column label="订单号" align="center" prop="number" />
      <el-table-column label="客户编号" align="center" prop="cusId" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="订单日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款方式" align="center" prop="payment" />
      <el-table-column label="付款金额" align="center" prop="money" />
      <el-table-column label="商品类型" align="center" prop="type" />
      <el-table-column label="销售员编号" align="center" prop="userid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cashier:edit']"
          >结账</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cashier:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="image = true"
        >支付方式</el-button>
        <el-drawer
          title="选择支付方式"
          :visible.sync="image"
          direction="rtl"
          size="50%">
          <el-image
            style="width: 500px; height: 500px"
            :src="url"
            :preview-src-list="srcList">
          </el-image>
        </el-drawer>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收据管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="number">
          <el-input v-model="form.number" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="客户编号" prop="cusId">
          <el-input v-model="form.cusId" placeholder="请输入客户编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="订单日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择订单日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="付款方式" prop="payment">

          <el-select v-model="form.payment" filterable placeholder="请选择付款方式">
            <el-option
              v-for="item in payments"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="商品类型" prop="type">
          <el-select v-model="form.type" filterable placeholder="请选择商品类型">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售员编号" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入销售员编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCashier, getCashier, delCashier, addCashier, updateCashier } from "@/api/system/cashier";

export default {
  name: "Cashier",
  data() {
    return {
      //预览大图显示支付方式
      image:false,
      url: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.Fchmj20pEe-kaEbhvIrt-gHaFy?w=183&h=143&c=7&r=0&o=5&dpr=1.4&pid=1.7',
      srcList: [
        'https://tse4-mm.cn.bing.net/th/id/OIP-C.Fchmj20pEe-kaEbhvIrt-gHaFy?w=183&h=143&c=7&r=0&o=5&dpr=1.4&pid=1.7'
      ],
      //商品类型下拉框选项数据
      options: [{
        value: '运动产品',
        label: '运动产品'
      }, {
        value: '食品',
        label: '食品'
      }, {
        value: '服饰',
        label: '服饰'
      }, {
        value: '日用品',
        label: '日用品'
      }, {
        value: '电子产品',
        label: '电子产品'
      }, {
        value: '书籍',
        label: '书籍'
      }],
//付款方式下拉框选项数据
      payments: [{
        value: '微信支付',
        label: '微信支付'
      }, {
        value: '支付宝支付',
        label: '支付宝支付'
      }, {
        value: '现金支付',
        label: '现金支付'
      }],
      value: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收据管理表格数据
      cashierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receipt: null,
        number: null,
        cusId: null,
        name: null,
        unit: null,
        quantity: null,
        price: null,
        date: null,
        payment: null,
        money: null,
        type: null,
        userid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        number: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        cusId: [
          { required: true, message: "客户编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "订单日期不能为空", trigger: "blur" }
        ],
        payment: [
          { required: true, message: "付款方式不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "付款金额不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "商品类型不能为空", trigger: "change" }
        ],
        userid: [
          { required: true, message: "销售员编号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询收据管理列表 */
    getList() {
      this.loading = true;
      listCashier(this.queryParams).then(response => {
        this.cashierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        receipt: null,
        number: null,
        cusId: null,
        name: null,
        unit: null,
        quantity: null,
        price: null,
        date: null,
        payment: null,
        money: null,
        type: null,
        userid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.receipt)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收据管理";
      //
    },
    /** 结账按钮操作 */
    handleUpdate(row) {
      this.reset();
      const receipt = row.receipt || this.ids
      getCashier(receipt).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "结账收据管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.receipt != null) {
            updateCashier(this.form).then(response => {
              this.$modal.msgSuccess("结账成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCashier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const receipts = row.receipt || this.ids;
      this.$modal.confirm('是否确认删除收据管理编号为"' + receipts + '"的数据项？').then(function() {
        return delCashier(receipts);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cashier/export', {
        ...this.queryParams
      }, `cashier_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
